        
        dojo.require('dojo.parser');
        dojo.require('dojo.data.ItemFileReadStore');
        dojo.require('dojo.data.ItemFileWriteStore');
        dojo.require('dojox.grid.compat._grid.edit');
        dojo.require('dojox.grid.DataGrid');
        dojo.require('dijit.form.Button');
        dojo.require('dijit.Dialog');
		
		var idx=0;
		
		function setfeaturedItemsValue(str)
		{
			var featureds =	document.getElementById('featuredItems');
			featureds.value = str;
		}
		
		function updatefeaturedItemsField()
		{
			var cartXML =	document.getElementById('cartXML');
			cartXML.value = collectItemsFromfeatured();	
		}
		
        function itemHasChanged(item, attribute, oldValue, newValue)
        {
            if( oldValue == newValue ) return;
			var featuredGrid = dijit.byId('featuredGrid_id');
            featuredGrid.store.setValue( item, attribute, newValue );
			var value = featuredGrid.store.getValue( item, attribute );
			updatefeaturedItemsField();
        }
         
        function getSelectedCatalogItems()
        {
 	      	if(null == catalogGrid.selection) return null;
             var items = catalogGrid.selection.getSelected();
            return items;
        }

        function getSelectedfeaturedItems()
        {
            var items = featuredGrid.selection.getSelected();
            return items;
        }

        function addSelectedItemTofeatured()
        {
            var selectedItems = getSelectedCatalogItems();
            if(null != selectedItems && selectedItems.length<1)
            if(selectedItems.length<1)
            {
                alert('You have to select an item in the merchandise catalog first.');
                return;
            }
			
			var alreadyThere = false;
            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
                var id = selectedItem.id[0]
                var name = selectedItem.name[0];
                var price = selectedItem.price[0];
				var category = selectedItem.category[0];
				
				for( var j=0; j < featuredArray.items.length; j++ )
				{
					if( id == featuredArray.items[j].id )
					{
						alreadyThere = true;
						break;
					}
				}
            }
			
			if (alreadyThere) 
			{
				alert('Some of the merchandise you selected is already in the featured items list.');
				return;
			}
			
            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
                var id = selectedItem.id[0]
                var name = selectedItem.name[0];
                var price = selectedItem.price[0];
				var category = selectedItem.category[0];

                featuredGrid.store.newItem({ idx: idx++, 
						id: id, 
						name:name, 
						category:category, 
						price:price,
						picture:''
					 });
            }
			
			updatefeaturedItemsField();
        }

        function removeSelectedItemFromfeatured()
        {
            var selectedItems = getSelectedfeaturedItems();
            if(selectedItems.length<1)
            {
                alert('You have to select an item before you can remove it.');
                return;
            }

            var selectedItem = selectedItems[0];
            featuredGrid.store.deleteItem(selectedItem);
			featuredGrid.selection.clear();
			updatefeaturedItemsField();
        }

        function collectItemsFromfeatured()
        {
            // can also just make this an XML string and send it...
            var xmlStr = '<featuredItems>';
            for( var i=0; i < featuredArray.items.length; i++ )
            {
                xmlStr = xmlStr + '<featuredItem>';
                xmlStr = xmlStr + '<price_list_id>' + featuredArray.items[i].id + '</price_list_id>';
                xmlStr = xmlStr + '<name>' + featuredArray.items[i].name + '</name>';
                xmlStr = xmlStr + '<price>' + featuredArray.items[i].price + '</price>';
                xmlStr = xmlStr + '<picture>' + featuredArray.items[i].picture + '</picture>';
                xmlStr = xmlStr + '</featuredItem>';
            }
            var xmlStr = xmlStr + '</featuredItems>';
            return xmlStr;
        }

        var catalogStore = new dojo.data.ItemFileReadStore({data:catalogArray});
        var featuredStore = new dojo.data.ItemFileWriteStore({data:featuredArray});
		
        dojo.addOnLoad( function() {

        var catalogStore = new dojo.data.ItemFileWriteStore({data:catalogArray});
        var featuredStore = new dojo.data.ItemFileWriteStore({data:featuredArray});
		
		catalogGrid = dijit.byId('catalogGrid_id');
		featuredGrid = dijit.byId('featuredGrid_id');
		
		updatefeaturedItemsField();
					
		var gotCatalogItems = function(items, request){
		    for (var i = 0; i < items.length; i++){
		       var item = items[i];
		       console.log('Located item: ' + store.getLabel(item));
		    }
		}
		var request = catalogStore.fetch({query: {type:'id'}, onComplete: gotCatalogItems});
        dojo.connect( featuredGrid.store, "onSet", itemHasChanged );
		
		//dojo.connect( featuredGrid.store, "onSet", itemHasChanged );
});
